<link rel="import" href="../../bower_components/polymer/polymer.html">
<link rel="import" href="../../bower_components/paper-styles/typography.html">

<link rel="import" href="../app-form/app-form.html">

<dom-module id="cloud-edit">
    <template>
        <style include="shared-styles forms">

        </style>
        <p>Editing the cloud's credentials may result in unexpected behaviour. Proceed with caution.</p>
        <app-form fields="{{fields}}" url="/api/v1/clouds/[[cloud.id]]" method="PATCH" on-request="_editCloudRequest" on-response="_editCloudsResponse"
            btncontent="Edit Credentials" show-cancel></app-form>

    </template>
    <script>
        Polymer({
            is: 'cloud-edit',
            properties: {
                keys: {
                    type: Array
                },
                clouds: {
                    type: Object
                },
                cloud: {
                    type: Object
                },
                fields: {
                    type: Array,
                    computed: "_computeFields(cloud.*)",
                    notify: true
                }
            },
            observers: [
                '_fillInCreds(cloud)'
            ],
            _computeFields: function (cloud) {
                var providerFields = PROVIDERS.filter(function (fields) { // FIXME: Don't depend on global var
                    if (this.cloud && this.cloud.provider)
                        return fields.val == this.cloud.provider;
                }, this);
                // don't allow title and region in the fields
                var fields = [];
                if (providerFields[0] && providerFields[0].options) {
                    fields = providerFields[0].options.filter(function (f) {
                        return f.name != 'title' && f.name != 'region' && f.name != 'dns_enabled'
                    });

                    for (var i = 0; i < fields.length; i++) {
                        var name = fields[i].name;
                        if (this.cloud[name]) {
                            this.set('fields.' + i + '.value', this.cloud[name]);
                        }
                        //if docker_host, search for clouds host
                        if (name == 'docker_host' && this.cloud['host']) {
                            fields[i].value = this.cloud['host']
                        }
                        //if docker_port, search for clouds host
                        else if (name == 'docker_port' && this.cloud['port']) {
                            fields[i].value = this.cloud['port']
                        }
                        //if there is a key
                        else if (fields[i].type == 'ssh_key') {
                            fields[i].options = this.keys;
                        } else if (fields[i].type == 'list') {
                            var field = fields[i];
                            for (var j = 0; j < field.options.length; j++) {
                                if (field.options[j].type == 'ssh_key') {
                                    field.options[j].options = this.keys;
                                }
                            }
                        }
                    }
                }
                return fields;
            },
            _fillInCreds: function (cloud) {
                if (cloud) {
                    //reset form 
                    if (this.shadowRoot.querySelector('app-form')) {
                        this.shadowRoot.querySelector('app-form').form = {};
                    }
                    // if there is an apikey we can fill in
                    var apikey = this.cloud.apikey;
                    var index = this._fieldIndexByName('apikey', this.fields);
                    if (apikey && index > -1) {
                        this.set('fields.' + index + '.value', apikey);
                        // if there is apikey and an apisecret we can 'getsecretfromdb'
                        var indexp = this._fieldIndexByName('apisecret');
                        if (indexp != undefined) {
                            this.set('fields.' + indexp + '.value', 'getsecretfromdb');
                        }
                    }

                    if (this.cloud.username) {
                        this._fillIn('username', 'machine_user');
                    }

                    if (this.cloud.host) {
                        this._fillIn('host', 'machine_hostname');
                    }

                    if (this.cloud.key) {
                        this._fillIn('key', 'machine_key');
                    }

                    if (this.cloud.provider == 'bare_metal' && this.cloud.machines) {
                        this.async(function () {
                            this._fillInHosts(Object.values(this.cloud.machines));
                        }.bind(this), 200);
                    }

                }
            },

            _fillInHosts: function (machines) {
                var lindex = this._fieldIndexByName('machines');
                this.set('fields.' + lindex + '.items', []);

                for (var i = 0; i < machines.length; i++) {
                    var opts = this.fields[lindex].options.slice(0);

                    this.push('fields.' + lindex + '.items', opts);
                    this.set('fields.' + lindex + '.items.' + i + '.' + 0 + '.value', machines[i].hostname);
                    this.set('fields.' + lindex + '.items.' + i + '.' + 1 + '.value', machines[i].name);
                    this.set('fields.' + lindex + '.items.' + i + '.' + 2 + '.value', machines[i].os_type);
                    this.set('fields.' + lindex + '.items.' + i + '.' + 3 + '.value', machines[i].key_associations[
                        0].key);

                    this.set('fields.' + lindex + '.items.' + i + '.' + 4 + '.value', machines[i].key_associations[
                        0].ssh_user);
                    this.set('fields.' + lindex + '.items.' + i + '.' + 5 + '.value', machines[i].key_associations[
                        0].port);
                    this.set('fields.' + lindex + '.items.' + i + '.' + 6 + '.value', machines[i].rdp_port);

                }
            },

            _fillIn: function (cloudProperty, fieldName) {
                var index = this._fieldIndexByName(fieldName, this.fields);
                if (this.cloud[cloudProperty] && index != undefined) {
                    this.set('fields.' + index + '.value', this.cloud[cloudProperty]);
                }
            },

            _fieldIndexByName: function (name, fields) {
                var index;
                if (this.fields) {
                    var passField = this.fields.find(function (f, ind) {
                        if (f.name == name)
                            index = ind;
                        return f.name == name;
                    });
                }
                return index;
            },
            fieldsOfType: function (data, type) {
                var typeIndexes = [];
                var fieldsOfType = data.filter(function (f, ind) {
                    if (f.type == type)
                        typeIndexes.push(ind);
                    return f.type == type;
                });
                return typeIndexes;
            },
            updateKeys: function (e) {
                var keyFieldsIndexes = this.fieldsOfType(this.fields, 'ssh_key');
                console.log('updateKeys', keyFieldsIndexes);
                this.async(function () {
                    for (var i = 0; i < keyFieldsIndexes.length; i++) {
                        this.set('fields.' + keyFieldsIndexes[i] + '.options', this.keys);
                        this.set('fields.' + keyFieldsIndexes[i] + '.value', e.detail.key);
                    }
                    if (this.fieldsOfType(this.fields, 'list')) {
                        this.updateKeysInLists(e, this.fieldsOfType(this.fields, 'list'));
                    }
                }.bind(this), 1000);
            },
            updateKeysInLists: function (e, lists) {
                for (var j = 0; j < lists.length; j++) {
                    var keyFieldsIndexes = this.fieldsOfType(this.fields[lists[i]].options, 'ssh_key');
                    console.log('updateKeys', this.fields[lists[i]].options);

                    this.async(function () {
                        for (var i = 0; i < keyFieldsIndexes.length; i++) {
                            this.set('fields.' + lists[i] + '.options.' + keyFieldsIndexes[i] +
                                '.options', this.keys);
                            this.set('fields.' + lists[i] + '.options.' + keyFieldsIndexes[i] +
                                '.value', e.detail.key);
                        }
                    }.bind(this), 500);
                }
            },
            _editCloudRequest: function () {},
            _editCloudsResponse: function () {},
        });
    </script>
</dom-module>